package com.rbac.interfaces.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rbac.application.service.OperationLogService;
import com.rbac.common.result.Result;
import com.rbac.domain.dto.query.OperationLogPageQuery;
import com.rbac.domain.entity.OperationLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "操作日志管理", description = "操作日志相关接口")
@RestController
@RequestMapping("/monitor/log")
@RequiredArgsConstructor
public class OperationLogController {

    private final OperationLogService operationLogService;

    @Operation(summary = "分页查询操作日志")
    @GetMapping("/page")
    @SaCheckPermission("monitor:log:query")
    public Result<IPage<OperationLog>> pageOperationLogs(OperationLogPageQuery query) {
        return Result.success(operationLogService.pageOperationLogs(query));
    }

    @Operation(summary = "获取操作日志详情")
    @GetMapping("/{id}")
    @SaCheckPermission("monitor:log:detail")
    public Result<OperationLog> getLogById(@PathVariable Long id) {
        return Result.success(operationLogService.getLogById(id));
    }

    @Operation(summary = "清理旧日志")
    @DeleteMapping("/clean/{days}")
    @SaCheckPermission("monitor:log:query")
    public Result<Void> cleanOldLogs(@PathVariable int days) {
        operationLogService.cleanOldLogs(days);
        return Result.success("清理完成");
    }

    @Operation(summary = "导出操作日志")
    @PostMapping("/export")
    @SaCheckPermission("monitor:log:export")
    public Result<Void> exportLogs(@RequestBody OperationLogPageQuery query) {
        operationLogService.exportLogs(query);
        return Result.success("导出成功");
    }
}